package com.woniu.tms.web.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * 权限拦截器
 */
@Component
public class PermissionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Set<String> userPermissions = (Set<String>) request.getSession().getAttribute("userPermissions");
        String uri = request.getRequestURI().trim();
        String subUrl = uri.substring(1);
        for (String userPermission : userPermissions) {
        	System.out.println("userPermission = " + userPermission);
        }
        System.out.println(uri);
        if(userPermissions.contains(subUrl)) {
        	return true;
        }
//        for (String str : userPermissions) {
//            if (subUrl.equals(str)) {
//                return true;
//            }
//        }
        response.sendRedirect(request.getServletContext().getAttribute("APP_PATH") + "/error.jsp");
        return false;
    }
}
